package com.kensure.shike.baobei.service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.DateUtils;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.dao.SKKanJiaDao;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.model.SKKanJia;
import com.kensure.shike.baobei.model.SKKanJiaDetail;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 发起砍价情况服务实现类
 * 
 * @author fankd created on 2019-7-1
 * @since
 */
@Service
public class SKKanJiaService extends JSBaseService {

	@Resource
	private SKKanJiaDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKBaobeiService sKBaobeiService;
	@Resource
	private SKUserService sKUserService;
	@Resource
	private SKKanJiaDetailService sKKanJiaDetailService;

	public SKKanJia selectOne(Long id) {
		return dao.selectOne(id);
	}

	public SKKanJia getFullOne(Long id) {
		SKKanJia kanjia = selectOne(id);
		SKUser user = sKUserService.selectOne(kanjia.getUserid());
		List<SKKanJiaDetail> helps = sKKanJiaDetailService.getKanJia(kanjia.getId());
		kanjia.setUser(user);
		kanjia.setHelps(helps);
		return kanjia;
	}

	public List<SKKanJia> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKKanJia> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKKanJia obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_kanjia"));
		obj.setKjcs(0);
		obj.setStatus(1);
		return dao.insert(obj);
	}

	public boolean update(SKKanJia obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	/**
	 * 获取是否发起过砍价
	 * 
	 * @param userid
	 * @param bbid
	 * @return
	 */
	public SKKanJia getKanJia(Long userid, Long bbid) {
		Map<String, Object> parameters = MapUtils.genMap("userid", userid, "bbid", bbid);
		List<SKKanJia> list = selectByWhere(parameters);
		SKKanJia kanjia = null;
		if (CollectionUtils.isNotEmpty(list)) {
			kanjia = list.get(0);
		}
		return kanjia;
	}

	/**
	 * 获取今天发起的砍价
	 * 
	 * @return
	 */
	public SKKanJia getKanJiaToday(Long userid) {
		// 24小时内的砍价数量
		Date now = new Date();
		now = DateUtils.getPastDay(now, -1);
		Map<String, Object> parameters = MapUtils.genMap("userid", userid, "startCreatedTime", now);
		List<SKKanJia> list = selectByWhere(parameters);
		SKKanJia kanjia = null;
		if (CollectionUtils.isNotEmpty(list)) {
			kanjia = list.get(0);
		}
		return kanjia;
	}

	/**
	 * 发起过砍价
	 * 
	 * @param userid
	 * @param bbid
	 * @return
	 */
	public synchronized SKKanJia add(Long userid, Long bbid) {
		SKBaobei baobei = sKBaobeiService.selectOne(bbid);
		if (baobei.getHdtypeid() != 2) {
			BusinessExceptionUtil.threwException("该商品不允许砍价");
		}
		Date now = new Date();
		if (now.compareTo(baobei.getTimeExpire()) > 0) {
			BusinessExceptionUtil.threwException("该商品砍价活动已经结束");
		}

		SKKanJia kanjia = getKanJia(userid, bbid);
		if (kanjia != null) {
			BusinessExceptionUtil.threwException("您已经发起该商品的砍价");
		}
		kanjia = getKanJiaToday(userid);
		if (kanjia != null) {
			BusinessExceptionUtil.threwException("24小时内，只能发起一次砍价申请");
		}

		kanjia = new SKKanJia();
		kanjia.setBbid(bbid);
		kanjia.setUserid(userid);
		kanjia.setStartPrice(baobei.getSalePrice());
		kanjia.setCurPrice(kanjia.getStartPrice());
		insert(kanjia);
		return kanjia;
	}

	/**
	 * 砍价使用
	 * 
	 * @return
	 */
	public synchronized void userKanJia(Long id) {
		SKKanJia kanjia = selectOne(id);
		if (kanjia.getStatus() != 1) {
			BusinessExceptionUtil.threwException("砍价券无法使用");
		}
		SKBaobei baobei = sKBaobeiService.selectOne(kanjia.getBbid());
		Date now = new Date();
		if (now.compareTo(baobei.getTimeExpire()) > 0) {
			BusinessExceptionUtil.threwException("该商品砍价活动已经结束");
		}
		kanjia.setStatus(9);
		update(kanjia);
	}

}
